package com.bmp.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bmp.model.Cases;
import com.bmp.model.vo.CaseVoProjectUser;
import com.bmp.service.CasesManager;
import com.opensymphony.xwork2.ActionSupport;

public class CasesAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private CasesManager casesManager;
	private Cases cases;
	
	private JSONObject jsonObject;
	private String page;
	private String rows;
	
	//初始化casesManager
	public CasesAction(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		casesManager = (CasesManager) ctx.getBean("casesManager");
	}
	
	//显示用例列表，json格式
	public String casesInfo(){
		//当前页  
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数          
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        //每页的开始记录  第一页为1  第二页为number +1   
        int start = (intPage-1)*number;  
        List<CaseVoProjectUser> casesList = casesManager.getCases(start, number);//每页的数据，放入list        
        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
        jsonMap.put("total", casesManager.getTotalPages());//total键 存放总记录数，必须的     
        jsonMap.put("rows", casesList);//rows键 存放每页记录 list  
        jsonObject = JSONObject.fromObject(jsonMap);//格式化result   一定要是JSONObject  
		return "casesInfo";
	}
	
	//增加用例
	public String addCases(){
		casesManager.addCases(this.cases);
		return "success";
	}
	
	//删除用例
	public String deleteCases(){
		casesManager.deleteCasesById(this.cases.getId());
		return "success";
	}
	
	//更新用例信息
	public String updateCases(){
		casesManager.updateCases(this.cases);
		return "success";
	}
	
	/**********************getter and setter***************************/
	public CasesManager getCasesManager() {
		return casesManager;
	}
	public void setCasesManager(CasesManager casesManager) {
		this.casesManager = casesManager;
	}
	public Cases getCases() {
		return cases;
	}
	public void setCases(Cases cases) {
		this.cases = cases;
	}
	public JSONObject getJsonObject() {
		return jsonObject;
	}
	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
}
